﻿namespace CoreLibrary.Email.Impl.Mandrill {

    public class MailBuilder {
        
        private readonly Mail _mail = new Mail();

        public MailBuilder() {
            _mail.important    = true;
            _mail.track_clicks = false;
            _mail.track_opens  = false;
        }

        public Mail Build() {
            return _mail;
        }

        public MailBuilder SetSender(string name, string address) {
            _mail.from_name  = name;
            _mail.from_email = address;
            return this;
        }

        public MailBuilder SetSubject(string subject) {
            _mail.subject = subject;
            return this;
        }

        public MailBuilder SetImportant(bool important) {
            _mail.important = important;
            return this;
        }

        public MailBuilder SetHtmlBody(string htmlBody) {
            _mail.html = htmlBody;
            return this;
        }

        public MailBuilder SetTextBody(string textBody) {
            _mail.text = textBody;
            return this;
        }

        public MailBuilder SetReplyEmailAddress(string replyEmailAddress) {
            _mail.AddHeader("Reply-To", replyEmailAddress);
            return this;
        }

        public MailBuilder WithTrackOpens(bool trackOpens) {
            _mail.track_opens = trackOpens;
            return this;
        }

        public MailBuilder WithTrackClicks(bool trackClicks) {
            _mail.track_clicks = trackClicks;
            return this;
        }

        public MailBuilder AddRecipient(MailRecipient recipient) {
            _mail.AddRecipient(recipient);
            return this;
        }

        public MailBuilder AddRecipient(string name, string email) {
            _mail.AddRecipient(new MailRecipient {
                name  = name,
                email = email
            });
            return this;
        }

        public MailBuilder AddMetaData(string name, string value) {
            if (!string.IsNullOrEmpty(name) 
             && !string.IsNullOrEmpty(value)) {
                 _mail.AddMetaData(name, value);
            }
            return this;
        }

        public MailBuilder AppendMailAttachment(MailAttachment attachment) {
            if (attachment != null) {
                _mail.AddAttachement(attachment);
            }
            return this;
        }
    }
}